package com.imooc.service.impl;

import com.imooc.bo.PeopleBO;
import com.imooc.enums.Sex;
import com.imooc.mapper.PeopleMapper;
import com.imooc.pojo.People;
import com.imooc.service.UserInterface;
import com.imooc.utils.DateUtils;
import com.imooc.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;

@Service
public class UserServiceImpl implements UserInterface {

    @Autowired
    private PeopleMapper peopleMapper;

    public static final  String USER_FACE = "";


    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Boolean queryUserNameIsExit(String username) {
        Example userExample = new Example(People.class);
        Example.Criteria criteria = userExample.createCriteria();
        criteria.andEqualTo("username",username);
        People result = peopleMapper.selectOneByExample(userExample);

        return result == null ? false : true;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public People createPeople(PeopleBO peopleBO) {
        People people = new People();
        people.setId(String.valueOf(System.currentTimeMillis()));
        people.setUsername(peopleBO.getUsername());
        people.setPassword(MD5Utils.MD5(peopleBO.getPassword()));
        // 默认用户昵称同用户名
        people.setNickname(peopleBO.getUsername());
        // 默认头像
        people.setFace(USER_FACE);
        // 默认生日
        people.setBirthday(DateUtils.parseDate("1994-05-15"));
        // 默认性别为保密
        people.setSex(Sex.secret.type);

        people.setCreatedTime(new Date());
        people.setUpdatedTime(new Date());

        peopleMapper.insert(people);
        return people;
    }
}
